<?php
//overhead.php
//This file contains the necessary overhead for loading any pages. This includes: DB connections, 
//accepting information from Sakai, and other common functions.
//Created by: Ryan Sloan (rsloan3@gatech.edu) - 3/17/2011

//We don't want anyone to be able to access this file by itself, so we add in some quick access control stuff - rjs
if (!isset($allow)) { $allow=false; } //$allow MUST be set to "true" in any file that will be accessed externally. - rjs
if ($allow) {
	session_start();
	//Handle DB connection. - rjs
	$db_schema = "cs4911_team4";
	$db_username = "cs4911_team4";
	$db_password = "FDIDYlJ0";
	$db = mysql_connect("academic-mysql.cc.gatech.edu",$db_username,$db_password);
	if (!mysql_select_db($db_schema)) {
		throwError(1); //Failure to connect to DB: Error code 1.
	}
	
	//The user ID, course ID and course role will be inherited from LTI. - rjs
	
	require_once 'ims-blti/blti.php';


	$context = new BLTI("secret", true, false);
	
	if (($context->valid) || (isset($_SESSION['currentUser']))) {
		if ($context->valid) {
			$currentUser = 	$context->getUserID();
			$currentUserName = $context->getUserName();
			$currentCourse = $context->getCourseName();
			$currentRole = ($context->isInstructor() ? "instructor" : "student");
			$_SESSION['currentUser']=$currentUser;
			$_SESSION['currentUserName']=$currentUserName;
			$_SESSION['currentCourse']=$currentCourse;
			$_SESSION['currentRole']=$currentRole;
		} else {
			$currentUser=$_SESSION['currentUser'];
			$currentUserName=$_SESSION['currentUserName'];
			$currentCourse=$_SESSION['currentCourse'];
			$currentRole=$_SESSION['currentRole'];
		}
	} else { 
		print "Error acquiring LTI data.";
		exit;
	}
} else {
	print "Access Denied.";
	exit;
	

}

//Error handling (crude ATM) -- rjs
function throwError($errorNumber) {
	//Error code 1: Database error.
	//Error code 2: Access denied error.
	//Error code 3: Could not create assignment Directory.
	print $currentRole;
	print "An error (".$errorNumber.") has occurred.";
	exit;
}

function printBreadcrumbs() {
	global $breadcrumbs,$pageTitle;
	foreach ($breadcrumbs as $crumb) {
		print "<small><a href='".$crumb["link"]."'>".$crumb["title"]."</a> > </small>";
	}
	print "<small><strong>".$pageTitle."</strong></small>";
}

?>